thin.imports('/public/configure/conf.msg.js', function() {
	factory.request('_msg_volist');
});

$(function() {
	$('#msg-modal-list').on('click', '#btn-msgtzz', function() {
		var that = $(this),
			_id = that.attr('data-id'),
			proid = that.attr('data-proid'),
			isread = that.attr('data-isread'),
			ischat = that.attr('data-ischat');
		var actions_opt = [];
		actions_opt.push({
			text: "查看",
			onClick: function() {
				if(isread == 0) {
					$.showLoading();
					__getdata_msgread['id'] = _id;
					factory.request('_msg_read', function() {
						window.location.href = '/mobile/chain/project?proid=' + proid + '&msgid=' + _id + '&isPull=1#details';
					});
				} else {
					window.location.href = '/mobile/chain/project?proid=' + proid + '&msgid=' + _id + '&ischat=' + ischat + '&isPull=1#details';
				}
			}
		});
		if(ischat == 0) {
			actions_opt.push({
				text: "约谈",
				onClick: function() {
					$.showLoading();
					__getdata_msgchat['id'] = _id;
					factory.request('_msg_chat');
				}
			});
		}
		actions_opt.push({
			text: "拒绝",
			onClick: function() {
				$.showLoading();
				__getdata_msgrefuse['id'] = _id;
				factory.request('_msg_refuse', _id);
			}
		});
		$.actions({
			actions: actions_opt
		});
	}).on('click', '#btn-msgcyz', function() {
		var that = $(this),
			_id = that.attr('data-id'),
			proid = that.attr('data-proid'),
			isread = that.attr('data-isread');
		$.actions({
			actions: [{
				text: "查看",
				onClick: function() {
					window.location.href = '/mobile/chain/project?proid=' + proid + '#details';
				}
			}, {
				text: "撤回",
				onClick: function() {
					$.showLoading();
					__getdata_msgundo['id'] = _id;
					factory.request('_msg_undo', _id);
				}
			}]
		});
	});
});

function __complete_msgvolist(res) {
	$.hideLoading();
	if(res.status == 200) {
		if(res.data.list.length > 0) {
			createMsgList(res.data);
			$('.nothing-box').hide();
		} else {
			$('msg-modal-list').html('');
			$('.nothing-box').show();
		}
	} else {
		$('.nothing-box').show();
		$.toptip(res.error, 'error');
	}
}

function createMsgList(data) {
	var ele = '';
	var _role = tool.__cookie.get('memberRole');
	for(var i in data.list) {
		var list = data.list[i];
		ele += '<div class="msgpanel" data-id="' + list.id + '">';
		//ele += '<p class="fs14r text-center p-y-1x weui-line-reset">' + tool.formatDate('yy-mm-dd hh:ii:ss', list.create_time) + '</p>';
		if(_role >= 2) {
			// 投资人
			ele += '<div class="weui-flex text-center p-y-1x weui-line-reset">';
			ele += '<div class="weui-flex__item">';
			ele += '<img src="' + list.from_member_headimgurl + '" class="pic-circle" />';
			ele += '<p class="m-t-1x fs12r">' + list.from_member_nickname + '</p>';
			ele += '</div>';
			ele += '<div class="weui-flex__item">';
			if(list.is_read == 0) {
				// dcdcdc (未查阅)
				ele += '<div class="p-t-2x c-dcdcdc">';
				ele += '<i class="icon-plane fs32r"></i>';
				ele += '<p class="fs12r">未查阅</p>';
			} else if(list.is_chat == 0) {
				// e64340 (未约谈)
				ele += '<div class="p-t-2x c-e64340">';
				ele += '<i class="icon-plane fs32r"></i>';
				ele += '<p class="fs12r">未约谈</p>';
			} else {
				// 1aad19 (已约谈)
				ele += '<div class="p-t-2x c-1aad19">';
				ele += '<i class="icon-plane fs32r"></i>';
				ele += '<p class="fs12r">已约谈</p>';
			}
			ele += '<p class="fs12r">' + tool.formatDate('yy-mm-dd', list.create_time) + '</p>';
			ele += '</div>';
			ele += '</div>';
			ele += '<div class="weui-flex__item">';
			ele += '<img src="' + list.project.logo + '" class="pic-circle" />';
			ele += '<p class="m-t-1x fs12r">' + list.project.title + '</p>';
			ele += '</div>';
			ele += '</div>';
		} else {
			// 创业者
			ele += '<div class="weui-flex text-center p-y-1x weui-line-reset">';
			ele += '<div class="weui-flex__item">';
			ele += '<img src="' + list.project.logo + '" class="pic-circle" />';
			ele += '<p class="m-t-1x fs12r">' + list.project.title + '</p>';
			ele += '</div>';
			ele += '<div class="weui-flex__item">';
			if(list.is_read == 0) {
				// dcdcdc (未查阅)
				ele += '<div class="p-t-2x c-dcdcdc">';
				ele += '<i class="icon-plane fs32r"></i>';
				ele += '<p class="fs12r">未查阅</p>';
			} else if(list.is_chat == 0) {
				// e64340 (未约谈)
				ele += '<div class="p-t-2x c-e64340">';
				ele += '<i class="icon-plane fs32r"></i>';
				ele += '<p class="fs12r">未约谈</p>';
			} else {
				// 1aad19 (已约谈)
				ele += '<div class="p-t-2x c-1aad19">';
				ele += '<i class="icon-plane fs32r"></i>';
				ele += '<p class="fs12r">已约谈</p>';
			}
			ele += '<p class="fs12r">' + tool.formatDate('yy-mm-dd', list.create_time) + '</p>';
			ele += '</div>';
			ele += '</div>';
			ele += '<div class="weui-flex__item">';
			ele += '<img src="' + list.to_member_headimgurl + '" class="pic-circle" />';
			ele += '<p class="m-t-1x fs12r">' + list.to_member_nickname + '</p>';
			ele += '</div>';
			ele += '</div>';
		}

		if(_role >= 2) {
			// 投资人
			ele += '<div class="weui-flex text-center">';
			ele += '<div class="weui-flex__item p-y-2x b-line-r">';
			if(list.is_chat == 1) {
				ele += '<a href="tel:' + list.from_mobile + '" class="f-mj"><i class="icon-phone p-r-1x fs24r"></i>' + list.from_mobile + '</a>';
			}
			ele += '</div>';
			ele += '<div class="p-y-2x pw32 color-wx">';
			ele += '<a href="javascript:;" data-id="' + list.id + '" data-proid="' + list.project_id + '" id="btn-msgtzz" data-isread="' + list.is_read + '" data-ischat="' + list.is_chat + '">操作</a>';
			ele += '</div>';
			ele += '</div>';
		} else {
			// 创业者
			ele += '<div class="weui-flex text-center">';
			ele += '<div class="weui-flex__item p-y-2x b-line-r">';
			if(list.is_chat == 1) {
				//ele += '<a href="tel:' + list.to_mobile + '" class="f-mj"><i class="icon-phone p-r-1x fs24r"></i>' + list.to_mobile + '</a>';
			}
			ele += '</div>';
			ele += '<div class="p-y-2x pw32 color-wx">';
			ele += '<a href="javascript:;" data-id="' + list.id + '" data-proid="' + list.project_id + '" id="btn-msgcyz" data-isread="' + list.is_read + '" data-ischat="' + list.is_chat + '">操作</a>';
			ele += '</div>';
			ele += '</div>';
		}

		ele += '</div>';
	}
	$('#msg-modal-list').append(ele);
}

var __getdata_msgread = {},
	__getdata_msgundo = {},
	__getdata_msgchat = {},
	__getdata_msgrefuse = {};

function __complete_msgread(res, unkn) {
	$.hideLoading();
	if(res.status == 201) {
		unkn();
	} else {

	}
}

function __complete_msgchat(res) {
	$.hideLoading();
	if(res.status == 201) {
		jude_msgpage = false;
		$('#msg-modal-list').html('');
		checkTab('msg-modal');
	} else {

	}
}

function __complete_msgundo(res, unkn) {
	$.hideLoading();
	if(res.status == 204) {
		$('.msgpanel[data-id=' + unkn + ']').remove();
	}
}

function __complete_msgrefuse(res, unkn) {
	$.hideLoading();
	if(res.status == 204) {
		$('.msgpanel[data-id=' + unkn + ']').remove();
	}
}